package commands;

import java.util.LinkedList;
import java.util.List;

import core.Card;
import core.Zone;

/**
 * Represents the act of shuffling the <code>Card</code>s in a <code>Zone</code>.
 * Once this command executes, the id mappings for all cards in the shuffled zone
 * will have changed.
 */
public class ShuffleZoneCommand extends Command
{
	public ShuffleZoneCommand() {
		this(null);
	}

	public ShuffleZoneCommand(Zone z) {
		zone = z;
	}

	public net.StateUpdate execute() throws CommandException
	{
		net.StateUpdate stateUpdate = super.execute();
		List<Card> cards = new LinkedList<Card>(zone.getCards());
		java.util.Collections.shuffle(cards);
		java.util.Iterator<Card> old = zone.getCards().iterator();
		for (Card card : cards) {
			stateUpdate.addChange(new net.ChildRemoval(zone, "card", card));
			Card newCard = new Card(card);
			newCard.setLocation(old.next().getLocation());
			stateUpdate.addChange(new net.ChildAddition(zone, "card", newCard));
		}
		return stateUpdate;
	}

	public Zone getZone() {
		return zone;
	}

	public void setZone(Zone zone) {
		this.zone = zone;
	}

	public String getMessage() {
		return "shuffle " + util.Util.prettyPrint(zone);
	}

	private Zone zone;
}
